#!/bin/csh -f
if ( $#argv == 0 ) goto usage 
set SRCDIR="$DECAHOME/SRC/"
set fname=
set ticoargs=()
set tlhl=()
set qe=0 # extract-query
while( "$*" != "" )
    if ( -e $1 ) then # config name is not option
        set fname=$1
        shift
    endif
    if ( $#argv == 0 ) then
        goto skip_options
    endif
    switch ( $1 ) # collect valid tico arguments
	case -s:
	    set ticoargs=($ticoargs $1)
	breaksw
	case -p:
	    set ticoargs=($ticoargs $1)
	breaksw
	case -occ:
	    set ticoargs=($ticoargs $1)
	breaksw
	case -occ5:
	    set ticoargs=($ticoargs $1)
	breaksw
	case -e:
	    shift
	    set tlhl=($1)
	    shift
	    set tlhl=($tlhl $1)
	breaksw
	case -qe:
	    shift
	    set tlhl=($1)
	    shift
	    set tlhl=($tlhl $1)
	    set qe=1
	breaksw
	default: # assuming no-dash arguments - except filename, are tl/hl
	    echo "Unrecognized option $1"
	    goto usage
	breaksw
    endsw
    shift
end
skip_options:    
if($fname == "") goto nofile
# check if this is dfit many-copas file
set dfits=(`awk 'BEGIN{tlhl="0-0"}$1=="_newsample_"{ns++;tlhl=$2"-"$3}$1=="start_dfit_params"{tlhls=tlhls" "tlhl}END{print tlhls}' $fname`)
if ($#dfits > 0) then # this is dfit type file
    if($dfits[1] != "0-0" ) then # this is many-dfit-configs type
	# check if we have tlhl set
	if( $#tlhl == 2 ) then # yes: extracting one copa.dfit
	    set tmp=$tlhl[1]"-"$tlhl[2]
	    set ok=
	    foreach x ( $dfits )
		if( $x == $tmp ) set ok=1
	    end
	    if( $ok ) then
		echo "Extracting sample $tlhl " > "/dev/stderr"
		if( $qe == 0 ) then
		    awk -f $DECAHOME/SRC/dfitcopas.awk $fname $tlhl
		else
		    awk -f $DECAHOME/SRC/dfitcopas.awk $fname $tlhl | $DECAHOME/scripts/printdfpars
		endif
		exit
	    else
		echo "Sample $tlhl does not exist in $fname. It contains:"
		goto dfit_info
		exit
	    endif
	else # print info
#	    set tinfo=(`awk 'FNR==3{print;exit}' $fname`)
#	    echo "Tiling: $tinfo[1] nodes, multi=$tinfo[2], deco $tinfo[3]"
	    echo ":::::: file $fname is type _dfit.copas_:"
	    awk '$1=="info"{sub(/info /,"");print}$1=="end_fit_schedule"{pr=0}(pr>0){print}$1=="start_fit_schedule"{pr=1}' $fname
	    dfit_info:
	    echo "TL HL      E      Ueff       R      wR"
	    awk '$1=="_newsample_"{sub(/_newsample_ /,"");printf("%2i %2i %8.5f %8.5f %8.5f %8.5f\n",$1,$2,$3,$4,$5,$6)}' $fname
#	    echo "---------->>>>>>>>> tico:"
#	    echo -n "$fname "
#	    awk -f $DECAHOME/SRC/dfitcopas.awk $fname $tlhl | awk -f $SRCDIR/tico.awk
	    exit
	endif
    else # this is usual copa.dfit type with one dfit-param section in the end
	echo "This is _copa.dfit_ type file"
    endif
else
    if($#tlhl > 0) echo "Warning option -e: this is not dfit.copas type file"
endif 
########## finally the tico test
awk -f $SRCDIR/tico.awk $ticoargs $fname
##
exit
############## usage
nofile:
echo "Need to specify copafile."
usage:
echo "Synopsis: tico [-s] [-p] [-occ[5]] [-e tl hl] config_name"
echo "-p : show parameter section only"
echo "-s : silent (reports tinp, and error number, 0=OK)"
echo "-occ : show occupancy; assumes 5D representation is OFF!"
echo "-occ5 : show occupancy; assumes 5D representation is ON!"
echo "-e tl hl : extract sample tloop=tl hloop=hl from dfit.copas type file"
echo "-qe tl hl : as -e, but print only DW fit parameters"


